import {Component, OnInit} from '@angular/core';
import {VideoService} from '@app/core/http/video.service';
import {AppState, CustomerConfigService} from '@app/core/public-api';
import {Store} from '@ngrx/store';
import {HttpClient} from '@angular/common/http';

@Component({
  selector: 'tb-video-import',
  templateUrl: './video-import.component.html',
  styleUrls: ['./video-import.component.scss']
})
export class VideoImportComponent implements OnInit {

  cams: any[];
  displayedColumns = ['ck', 'cameraId', 'cameraIp', 'cameraName', 'comp'];

  constructor(private http: HttpClient, private store: Store<AppState>, private VideoService: VideoService, private customerconfigService: CustomerConfigService) {
  }

  batchAdd() {
    console.log('batchadd')
    this.cams.filter(i => i.ck).forEach(i => {
      console.log(i)
      this.VideoService.saveVideo({
        "name": i.cameraName,
        cameraId: i['cameraId'],
        ip: i.cameraIp,
      }).subscribe(e => {
        i['comp'] = true
        console.log(e)
      })
    })

  }

  AllChecked = false;

  toggleSelectAll() {

    this.cams.forEach(i => {
      i.ck = !this.AllChecked
    })
    this.AllChecked = !this.AllChecked

  }

  ngOnInit(): void {

    let orgcode = this.customerconfigService.getConfig().orgConfig.tenant.title
    this.http.get('/api/tenant_ip/getCameras', { params: { orgcode } }).subscribe(e=>{
      const response = e as any;
      let cams = []
      for (let index = 0; index < response.length; index++) {
    
        const cameraInfo = e[index];
        let t = {cameraId: cameraInfo.target.cameraid, cameraIp: cameraInfo.target.cameraip, cameraName: cameraInfo.target.name}
        cams.push(t)
      }
      this.cams = cams;
    })


  }

}
